package com.ash.print.template;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import lombok.Data;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.util.HashMap;

/**
 * 入库标签
 */
public class InTemplate implements Printable {

    private final TagInfo tagInfo;

    public InTemplate(TagInfo tagInfo) {
        this.tagInfo = tagInfo;
    }


    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
        Graphics2D g2d = (Graphics2D) graphics;
        if (pageIndex > 0) { // 如果不是第一页，则已经打印完毕
            return NO_SUCH_PAGE;
        }

        // 转换坐标系以适应页面格式
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        // 获取可打印区域的宽度和高度
        int width = (int) pageFormat.getImageableWidth();
        int height = (int) pageFormat.getImageableHeight();


        // 设置背景色（可选）
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, width, height);

        // 边框宽度为2个单位
        g2d.setStroke(new BasicStroke(2f));

        // 绘制边框
        g2d.setColor(Color.black);
        g2d.drawRect(5, 5, width - 20, height - 120);

        g2d.drawString("入库标签", 80, 20); //
        g2d.drawImage(qrcode(tagInfo.getExpressNo(), width, height), 10, 30, width - 30, height / 6, null);

        g2d.drawString("入库货架号：" + tagInfo.getShelvesNo(), 20, 120);
        g2d.drawString("取件号：" + tagInfo.getPickupCode(), 20, 140);
        g2d.drawString("收件人：" + tagInfo.getRecipientName(), 20, 160);
        g2d.drawString("打印时间：" + tagInfo.getPrintDateTime(), 20, 180);
        g2d.drawString("货物类型：" + tagInfo.getCargoType(), 20, 200);


        return PAGE_EXISTS;
    }

    public static BufferedImage qrcode(String barcodeData, int width, int height) {

        try {
            // 创建QRCodeWriter实例
            MultiFormatWriter barcodeWriter = new MultiFormatWriter();

            // 设置编码参数
            HashMap<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

            // 生成BitMatrix
            BitMatrix bitMatrix = barcodeWriter.encode(barcodeData, BarcodeFormat.CODE_128, width, height - 260, hints);

            // 将BitMatrix转换为BufferedImage
            BufferedImage bufferedImage = new BufferedImage(bitMatrix.getWidth(), bitMatrix.getHeight(), BufferedImage.TYPE_INT_RGB);
            for (int x = 0; x < bitMatrix.getWidth(); x++) {
                for (int y = 0; y < bitMatrix.getHeight(); y++) {
                    bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
                }
            }

            return bufferedImage;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Data
    public static class TagInfo {
        private String expressNo;
        private String shelvesNo;
        private String pickupCode;
        private String recipientName;
        private String printDateTime;
        private String cargoType;

    }
}
